package com.jingbian.cppccbackend.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jingbian.cppccbackend.entity.Activities;
import com.jingbian.cppccbackend.mapper.admin.adminActivitiesMapper;
import com.jingbian.cppccbackend.service.admin.ActivitiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingbian.cppccbackend.entity.vo.PageVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-02-14
 */
@Service
public class adminActivitiesServiceImpl extends ServiceImpl<adminActivitiesMapper, Activities> implements ActivitiesService {
    @Autowired
    private adminActivitiesMapper adminActivitiesMapper;

    @Override
    public PageVO<Activities> getActivities(Integer pageNum, Integer pageSize, String sortType, String startDate, String endDate, String status, String searchText) {
        PageVO<Activities> pb = new PageVO<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Activities> activities = adminActivitiesMapper.getActivities( sortType, startDate, endDate, status, searchText);
        Page<Activities> pag = (Page<Activities>) activities;
        pb.setTotal(pag.getTotal());
        pb.setItems(pag.getResult());
        return pb;
    }

    @Override
    public boolean updateStatus(Integer aid, String status) {
        return adminActivitiesMapper.updateStatus(aid, status) ;
    }

    @Override
    public Activities getActivityWithUserDetails(Integer aid) {
        return adminActivitiesMapper.getActivityWithUserDetails(aid);
    }

    @Override
    public List<Activities> getActivitiesForExport(String sortType, String startDate, String endDate, String status, String searchText) {
        return  adminActivitiesMapper.getActivities( sortType, startDate, endDate, status, searchText);
    }

    @Override
    public Map<String, Object> getActivityStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 获取活动总数
        Integer totalActivities = adminActivitiesMapper.getTotalActivities();
        
        // 获取昨日新增活动数
        Integer newActivitiesYesterday = adminActivitiesMapper.getNewActivitiesYesterday();
        
        // 获取活动成果总数
        Integer totalOutcomes = adminActivitiesMapper.getTotalOutcomes();
        
        statistics.put("totalActivities", totalActivities);
        statistics.put("newActivitiesYesterday", newActivitiesYesterday);
        statistics.put("totalOutcomes", totalOutcomes);
        
        return statistics;
    }
    @Override
    public List<Map<String, Object>> getActivityTrend(String startDate, String endDate) {
        return adminActivitiesMapper.getActivityTrend(startDate, endDate);
    }

    @Override
    public byte[] exportTrendData(String startDate, String endDate) throws IOException {
        List<Map<String, Object>> trendData = getActivityTrend(startDate, endDate);

        try (XSSFWorkbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("活动变化趋势");

            // 设置表头样式
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);

            // 创建表头
            Row headerRow = sheet.createRow(0);
            String[] header = {"日期", "新增活动数量"};

            for (int i = 0; i < header.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(header[i]);
                cell.setCellStyle(headerStyle);
                sheet.autoSizeColumn(i);
            }

            // 填充数据
            int rowNum = 1;
            for (Map<String, Object> data : trendData) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(data.get("date").toString());
                row.createCell(1).setCellValue(Integer.parseInt(data.get("value").toString()));
            }

            // 写入字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return outputStream.toByteArray();
        }
    }
}
